package com.xbongbong.saas.script.tempenum;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.CreditTemporaryApplyStatusEnum;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CreditTemporaryLimitEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author xingxing.xiao
 */
@Getter
public enum TempCreditTemporaryLimitDistributorEnum {

    BASE_INFO(CreditTemporaryLimitEnum.BASE_INFO),
    CREDIT_TEMPORARY_NO(CreditTemporaryLimitEnum.CREDIT_TEMPORARY_NO),
    CUSTOMER_ID(CreditTemporaryLimitEnum.CUSTOMER_ID),
    FIXED_LIMIT(CreditTemporaryLimitEnum.FIXED_LIMIT),
    APPLY_TEMPORARY_LIMIT(CreditTemporaryLimitEnum.APPLY_TEMPORARY_LIMIT),
    START_TIME(CreditTemporaryLimitEnum.START_TIME),
    END_TIME(CreditTemporaryLimitEnum.END_TIME),
    APPLY_NAME(CreditTemporaryLimitEnum.APPLY_NAME),
    APPLY_REASON(CreditTemporaryLimitEnum.APPLY_REASON),
    APPLY_TIME(CreditTemporaryLimitEnum.APPLY_TIME),
    APPLY_STATUS(CreditTemporaryLimitEnum.APPLY_STATUS),
    SYSTEM_INFO(CreditTemporaryLimitEnum.SYSTEM_INFO),
    CREATOR_ID(CreditTemporaryLimitEnum.CREATOR_ID),
    ADD_TIME(CreditTemporaryLimitEnum.ADD_TIME),
    UPDATE_TIME(CreditTemporaryLimitEnum.UPDATE_TIME),
    CUSTOMER_NAME(CreditTemporaryLimitEnum.CUSTOMER_NAME),
    APPLY_ID(CreditTemporaryLimitEnum.APPLY_ID),
    CREDIT_ID(CreditTemporaryLimitEnum.CREDIT_ID),
    APPLY_IS_DISTRIBUTOR(CreditTemporaryLimitEnum.APPLY_IS_DISTRIBUTOR),
    ;

    private CreditTemporaryLimitEnum anEnum;

    TempCreditTemporaryLimitDistributorEnum(CreditTemporaryLimitEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempCreditTemporaryLimitDistributorEnum> getAllEnum() {
        List<TempCreditTemporaryLimitDistributorEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static void initCreditTemporaryNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.CREDIT_TEMPORARY_LIMIT.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.CREDIT_TEMPORARY_LIMIT.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(CREDIT_TEMPORARY_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setSerialNumber(InitExplainsHelper.setSerialNumber(SerialNumberEnum.CREDIT_TEMPORARY_NO));
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setDistributorMark(DistributorMarkEnum.DISTRIBUTOR.getCode());
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }


    /**
     * 申请状态
     */
    public static void initApplyStatus(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsDictionary(1);
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(CreditTemporaryApplyStatusEnum.class, fieldAttrEntity.getFieldType());
        List<String> codeList = Arrays.asList(CreditTemporaryApplyStatusEnum.NORMAL.getName(), CreditTemporaryApplyStatusEnum.EXPIRED.getName(), CreditTemporaryApplyStatusEnum.REVOKED.getName());
        for (ItemPoJo itemPoJo: itemPoJos) {
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
            if (!codeList.contains(itemPoJo.getText())) {
                itemPoJo.setShowType(ShowTypeEnum.NONE.getCode());
                itemPoJo.setEditHide(1);
            } else {
                if (Objects.equals(itemPoJo.getText(), CreditTemporaryApplyStatusEnum.NORMAL.getName())) {
                    itemPoJo.setChecked(true);
                }
            }
        }
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_OPTION_RELY.getAlias(), ForbiddenSettingEnum.DISABLE_SELF_DEFINE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setItems(itemPoJos);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initCustomerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setLinkedText(CUSTOMER_NAME.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setDistributorMark(DistributorMarkEnum.DISTRIBUTOR.getCode());
    }

    /**
     * 固定额度
     * @param fieldAttrEntity
     */
    public static void initFixedLimit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initApplyTemporaryLimit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumericalLimits(new NumericalLimitsPoJo(0D, 100000000000D));
    }

    /**
     * 日期设置为当前时间
     * @param fieldAttrEntity
     */
    public static void initStartTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType( DateTimeEnum.TIME.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("today");
        defaultAttrPoJo.setDefaultValue("");
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
        // 限制时间选择
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setDateLimit(DateTimeUtil.getTodayInt(DateTimeUtil.getInt()) - BasicConstant.ONE);
        saasParticularAttributePoJo.setLimitType(false);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 日期设置
     * @param fieldAttrEntity
     */
    public static void initEndTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType(DateTimeEnum.TIME.getType());
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initApplyName(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initApplyReason(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 日期设置为当前时间
     * @param fieldAttrEntity
     */
    public static void initApplyTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType( DateTimeEnum.TIME.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("today");
        defaultAttrPoJo.setDefaultValue("");
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }


}
